<template>
  <div id="app">
    <div id="chart-container" ref="chartContainer" style="height: 100%; width: 100%; position: absolute; top: 10%; left: 0;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'EchartsDemo',
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart);
  },
  methods: {
    initChart() {
      const chartContainer = this.$refs.chartContainer;
      this.myChart = echarts.init(chartContainer, null, {
        renderer: 'canvas',
        useDirtyRect: false
      });

      const option = {
        tooltip: {
          confine: true,          // 强制提示框限制在图表区域内
          position: 'right',      // 优先右侧显示（根据实际情况调整）
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '预算分配',
            type: 'pie',
            radius: '50%',
            data: [
              { value: 335, name: '年度预算总产值' },
              { value: 310, name: '年度研发投入总预算' },
              { value: 234, name: '当前归集发生总额' },
              { value: 135, name: '投入差额' },
              { value: 1548, name: '归集发生额' },
              { value: 235, name: '研发投入预算' }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };

      if (option && typeof option === 'object') {
        this.myChart.setOption(option);
      }
    },
    resizeChart() {
      this.myChart.resize();
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
#chart-container {
  position: relative;
  height: 400px;
  overflow: hidden;
}
</style>
